package com.tcu.mosaicdemo;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * 从相册选择三张图片
 */
public class MainActivity extends AppCompatActivity {

    private static final int SELECT_PICTURE = 0;
    private static final int MAX_COUNT = 3;
    private TextView mChooseBtn;
    private Button mMosaicBtn;
    private LinearLayout mContainer;
    private ArrayList<String> mPathList = new ArrayList<String>();
    private int mCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mChooseBtn = (TextView) findViewById(R.id.choose_btn);
        mMosaicBtn = (Button) findViewById(R.id.mosaic_btn);
        mMosaicBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (3 == mCount) {
                    MosaicActivty.navToMosaicActivity(MainActivity.this, mPathList);
                } else {
                    Toast.makeText(MainActivity.this, "请选择三张图片！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mChooseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCount >= MAX_COUNT) {
                    Toast.makeText(MainActivity.this, "你已经选择了三张图片", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("image/*");
                    startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
                }
            }
        });
        mContainer = (LinearLayout) findViewById(R.id.text_container);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(uri, proj, null, null, null);

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            String path = cursor.getString(column_index);
            if (path != null && !TextUtils.isEmpty(path)) {
                TextView textView = new TextView(this);
                textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                textView.setText(path);
                mCount++;
                mContainer.addView(textView);
                mPathList.add(path);
            }
        } else {
            Toast.makeText(this, "请重新选择图片", Toast.LENGTH_SHORT).show();
        }

    }


}
